﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace WPFExcelReport
{

    public partial class TWBZZM
    {
        #region ==WB14KEY===========
        public static readonly DependencyProperty WB14KEYProperty = DependencyProperty.Register("WB14KEY",
            typeof(string), typeof(TWBZZM),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWB14KEYChanged));
        public string WB14KEY
        {
            get
            {
                return (string)GetValue(WB14KEYProperty);
            }
            set
            {
                SetValue(WB14KEYProperty, value); 
            }
        }
        private static void OnWB14KEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWBZZM;
            if (src != null)
            {
                src.WB14KEY = (string)e.NewValue;
            }
        }
        #endregion
        
        #region ==dwWBH===========
        public static readonly DependencyProperty dwWBHProperty = DependencyProperty.Register("dwWBH",
            typeof(DataRow), typeof(TWBZZM),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwWBHChanged));
        public DataRow dwWBH
        {
            get
            {
                return (DataRow)GetValue(dwWBHProperty);
            }
            set
            {
                SetValue(dwWBHProperty, value);
            }
        }
        private static void OndwWBHChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWBZZM;
            if (src != null)
            {
                src.dwWBH = (DataRow)e.NewValue;
            }
        }
        #endregion

        VWBBST WB;
        ITXEXRPT ITXEXRPT;
        public TWBZZM()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBBEW();
            this.DataContext = MV;
            InitializeComponent();
            DTSTART = MV.date.Substring(0, 6) + "01";
            DTEND = MV.date;

        }
        string filter = "";

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
        }
        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != Key.Enter)
                return;
            if (e.Key != Key.Return)
                return;
            QueryCommand(null, null);

        }
        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            { 
                dwData = TBMain.NewRow();
                dwData["WBEWMANDANT"] = app.Mandant;
                dwData["WBEWART"] = " AND WBEWART LIKE '"+ART.Trim()+"%'";
                dwData["WBEWFILLER"] = " AND WBEWBUCHDAT>=" + DTSTART + " AND WBEWBUCHDAT<= " + DTEND;                 
                SResult = TMV.QWBBEW10(dwData);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    row["ARTGRP"] = Helpers.Helper.Convert8859P1ToGB2312(row["ARTGRP"].ToString());
                    string str = row["CONSTRUCTION"].ToString();
                    int idx = -1;
                    idx=str.IndexOf(" ");
                    if(idx>0)
                    {
                        str = str.Substring(idx).Trim();
                        row["CONSTRUCTION"] = str;
                    } 
                }
                MergeData(SResult, true, true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

            base.QueryCommand(sender, e);
        }
        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                DTSTART = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                DTEND = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void ClearFilter_Click(object sender, RoutedEventArgs e)
        {
            cvMaster.CustomFilter = null;
        }

        private void Filter_Click(object sender, RoutedEventArgs e)
        {
            filter = "ARTICLE LIKE '" + txtART.Text.ToString().Trim() + "%'";
            cvMaster.CustomFilter = filter;
        }


        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            DataSet Set = new DataSet();
            DataTable tb = TBMain.Copy(); 
            tb.TableName = "WBZZM";
            Set.Merge(tb);
            cDR.DocType = "JLTWBZZM";
            cDR.PRINT(Set);
        }

        private void CHKWB14(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            WB14KEY = dwMaster["WBEWART"].ToString()+"  "+dwMaster["WBEWFARBE"].ToString(); 
        }
    }
}
